package com.cxg.fairy.ec.main.personal.coupon;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.order.PostOrderDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/18.
 */

public class AllCouponTabDelegate extends LatteDelegate {

    private final static String UUID = "UUID";

    private final static String ORDER_ID = "ORDER_ID";

    private static PostOrderDelegate mPostOrderDelegate = null;

    private static void setPostOrderDelegate(PostOrderDelegate delegate) {
        mPostOrderDelegate = delegate;
    }

    private static PostOrderDelegate getPostOrderDelegate() {
        return mPostOrderDelegate;
    }

    @BindView(R2.id.tab_layout)
    TabLayout mTabLayout = null;

    @BindView(R2.id.view_pager)
    ViewPager mViewPager = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    public static AllCouponTabDelegate create(int uuid, String preSubmitOrderId, PostOrderDelegate postOrderDelegate) {
        Bundle args = new Bundle();
        args.putInt(UUID, uuid);
        args.putString(ORDER_ID,preSubmitOrderId);
        setPostOrderDelegate(postOrderDelegate);
        AllCouponTabDelegate delegate = new AllCouponTabDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_coupon;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData();
        initTabLayout();
    }

    private void initTabLayout() {
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getContext(), R.color.favorListTag));
        mTabLayout.setTabTextColors(Color.parseColor("#565656"),Color.BLACK);
        mTabLayout.setBackgroundColor(Color.WHITE);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initPager(JSONObject obj) {
        final PagerAdapter adapter = new AllCouponTabPagerAdapter(getFragmentManager(),obj,this);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(adapter);
    }

    private void initData() {
        RestClient.builder()
                .url("getMsCouponGetByCate_num")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        LatteLogger.e("response=",response);
                        final int total = JSON.parseObject(response).getInteger("total");
                        if (total > 0) {
                            final JSONObject obj = JSON.parseObject(response).getJSONObject("data");
                            initPager(obj);
                        } else {
                            Toasty.info(getContext(), "当前帐户无可用优惠券!").show();
                        }
                    }
                })
                .build()
                .post();
    }


}
